package day29;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.text.DecimalFormat;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class MaterFrame extends JFrame {

	public static HashMap<String, MaterCode> map = new HashMap<String, MaterCode>(); 
	// public static Vector<MaterCode> mater = new Vector<MaterCode>();
	
	private String fileName = "noname.dat";
	private JPanel contentPane;

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MaterFrame frame = new MaterFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MaterFrame() {
		
		JLabel lblNewLabel = new JLabel("New label");
		getContentPane().add(lblNewLabel, BorderLayout.SOUTH);
		
		// 오브젝트(벡터) mater의 초기값
		map.put("품목코드1", new MaterCode("품목코드1", "품목이름1", 100, "품목스펙1",
				"품목고객1", 1000, 2000)); 
		map.put("품목코드2", new MaterCode("품목코드2", "품목이름1", 100, "품목스펙1",
				"품목고객2", 1000, 2000)); 
		map.put("품목코드3", new MaterCode("품목코드3", "품목이름1", 100, "품목스펙1",
				"품목고객3", 1000, 2000)); 
		map.put("품목코드4", new MaterCode("품목코드4", "품목이름1", 100, "품목스펙1",
				"품목고객4", 1000, 2000)); 
		map.put("품목코드5", new MaterCode("품목코드5", "품목이름1", 100, "품목스펙1",
				"품목고객5", 1000, 2000)); 
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("자재관리");
		menuBar.add(menu);
		
		
		private JMenuItem getMntmNewMenuItem_4() {
			if(mntmNewMenuItem_4 ==null){
				JMenuItem menuItem_3 = new JMenuItem("자재파일열기");
				menuItem_3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
									
				JFileChooser jc = new JFileChooser(fileName);	// filePathName은 디폴트값.
				int yn = jc.showOpenDialog(MaterFrame.this);
				if(yn == JFileChooser.APPROVE_OPTION) {			
					fileName = jc.getSelectedFile().getPath();
				
					
					// 파일에서 오브젝트까지 스트림으로 전달
					FileInputStream fos;
					ObjectInputStream ois;

					try {
						fos = new FileInputStream(fileName);
						ois = new ObjectInputStream(fos);
						
						map = (HashMap) ois.readObject();	// 오브젝트를 읽어서 MaterCode의 값을 가지는 벡터에 저장
												
						ois.close();		// 스트림 닫기
						fos.close();
						
					} catch (Exception ex) {
						textArea.setText("");	
						
						//labelSub.setText("파일을 읽는 도중에 에러가 발생하였습니다.");		// 오류 발생시 하단 label에 출력.
						// ex.printStackTrace();
						
						return;
					}
					
					
					//labelSub.setText("파일 열기를 완료하였습니다.");		// 하단 label에 안내 문구 출력					
					setTitle(fileName);
					
					
					String code, codeName, spec, customer;
					String strPrice, strImportEa, strExportEa;
					// int price, importEa, exportEa;
					
					
					// textArea에 파일에서 읽어 온 내용을 출력.

					textArea.setText("");				
					textArea.append(MaterFrame.dataTitle + "\n");
					textArea.append(MaterFrame.titleLine + "\n");
					

					for (int index = 0; index < MaterFrame.mater.size(); index++ ) {
						
						code = MaterFrame.mater.get(index).getCode();
						codeName = MaterFrame.mater.get(index).getCodeName();
						
						strPrice = dformat.format(MaterFrame.mater.get(index).getPrice());
						// strPrice = Integer.toString(MaterFrame.mater.get(index).getPrice());
						
						spec = MaterFrame.mater.get(index).getSpec();
						customer = MaterFrame.mater.get(index).getCustomer();
						
						strImportEa = dformat.format(MaterFrame.mater.get(index).getImportEa()); 
						strExportEa = dformat.format(MaterFrame.mater.get(index).getExportEa());		
										
						// strImportEa = Integer.toString(MaterFrame.mater.get(index).getImportEa());
						// strExportEa = Integer.toString(MaterFrame.mater.get(index).getExportEa());
						
						String tempStr = String.format(MaterFrame.format, code, codeName, strPrice, spec, customer, strImportEa, strExportEa);
							
						textArea.append(tempStr + "\n");
						
					} 
								
					// labelSub.setText("총 " + MaterFrame.mater.size() + "건의 데이터를 검색 하였습니다.");
									
				}
				
			}
		});
		menu.add(menuItem_3);
		
		JMenuItem menuItem_4 = new JMenuItem("파일저장");
		menu.add(menuItem_4);
		
		
		private 
		JMenuItem menuItem_5 = new JMenuItem("다른이름으로 저장");
		menu.add(menuItem_5);
		
		JMenuItem menuItem = new JMenuItem("조회");
		menu.add(menuItem);
		
		JMenuItem menuItem_1 = new JMenuItem("입고");
		menu.add(menuItem_1);
		
		JMenuItem menuItem_2 = new JMenuItem("출고");
		menu.add(menuItem_2);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("정보수정");
		menu.add(mntmNewMenuItem);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		JLabel lblNewLabel = new JLabel("그 다음 진행을 선택하세요");
		lblNewLabel.setBackground(Color.LIGHT_GRAY);
		lblNewLabel.setOpaque(true);
		contentPane.add(lblNewLabel, BorderLayout.SOUTH);
	}


			@Override 
			public void actionPerformed(ActionEvent arg0){
				String code = input.getCode().getText();
				String codeName = input.getCodeName().getText();
				String spec = input.getCodeName().getText();
				int price = Integer.parseInt(input.getPrice().getText());
				String spec = input.getSpec().getText();
				String customer = input.getCustomer().getText();
				int importEa = Integer.parsrINt(inpiut.getImportEa().getText());
				int exportEa= Integer.parseInt(int.getExportEa().getText());
			
			
			
			
			
